/*
* PROJECT: Happy New Year
* VERSION: 1.0
* AUTHOR: Mattias Thell
* URL: http://www.mattiasthell.com
*
* */

#include "Rocket.hpp"

#include "Settings.hpp"

namespace hny {

	Rocket::Rocket(World* world, const hgeVector& position, const hgeVector& velocity, int score, const hgeParticleSystemInfo& info)
		:Actor(world, Kind_Rocket), mHasFiredOnDeath(false)
	{
		hgeParticleSystemInfo pi = info;
		mParticleSystem = ParticlePtr(new hgeParticleSystem(&pi));

		Actor::SetPosition(position);
		Actor::SetVelocity(velocity);
		mParticleSystem->MoveTo(position.x, position.y);
		mParticleSystem->Fire();

		mScore = score;
	}

	Rocket::~Rocket()
	{
	}

	void Rocket::Update(float dt)
	{
		if(IsDying()) {
			if(mParticleSystem->GetParticlesAlive() <= 0)
				Kill();
		}
		else {
			int w = Settings::GetInstance()->SystemScreenWidth;
			int h = Settings::GetInstance()->SystemScreenHeight;
			int tolerance = 50; // Should go in settings....

			hgeVector pos = GetPosition();

			// Kill the rocket if it goes outside the screen
			if(pos.x > w + tolerance || pos.x < -tolerance || pos.y > h + tolerance || pos.y < -tolerance)
				StartDying();


			hgeVector newPos = pos + GetVelocity()*dt;
			SetPosition(newPos);

			mParticleSystem->MoveTo(newPos.x, newPos.y);
			
		}
		mParticleSystem->Update(dt);
	}

	void Rocket::Draw()
	{
		mParticleSystem->Render();
	}

	void Rocket::StartDying() 
	{
		SetIsDying();
		mParticleSystem->Stop();
	}

	int Rocket::GetScore() { return mScore; }
}